package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.ProductBean;
import com.ptmall.app.bean.TimeShopBean;
import com.ptmall.app.bean.model.HomePageIndex;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.TimeShopAdapter;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.ImageCycleView;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class TimeActivity extends BaseMvpActivity {


    PullableListView mListview;
    PullToRefreshLayout mRefreshView;

    TitleBarView titlebar;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    int page = 1;

    View headview;
    List<ProductBean> mDataLists = new ArrayList<>();
    TimeShopAdapter mTimeShopAdapter;

    TextView time, status, hour1, hour2, min1, min2, s1, s2;

    ImageCycleView time_slide;

    CountDownTimer countDownTimer;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            timer = timer - 1;
            try {

                hour1.setText(getTime(timer).charAt(0) + "");
                hour2.setText(getTime(timer).charAt(1) + "");
                min1.setText(getTime(timer).charAt(3) + "");
                min2.setText(getTime(timer).charAt(4) + "");
                s1.setText(getTime(timer).charAt(6) + "");
                s2.setText(getTime(timer).charAt(7) + "");
            } catch (Exception e) {

            }
        }
    };

    int timer = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_timeshop);
    }

    @Override
    public void initViews() {
        headview = LayoutInflater.from(getContext()).inflate(R.layout.layout_timeshoptop, null);
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        time = (TextView) headview.findViewById(R.id.time);
        status = (TextView) headview.findViewById(R.id.status);
        time_slide = (ImageCycleView) headview.findViewById(R.id.time_slide);
        hour1 = (TextView) headview.findViewById(R.id.hour1);
        hour2 = (TextView) headview.findViewById(R.id.hour2);
        min1 = (TextView) headview.findViewById(R.id.min1);
        min2 = (TextView) headview.findViewById(R.id.min2);
        s1 = (TextView) headview.findViewById(R.id.s1);
        s2 = (TextView) headview.findViewById(R.id.s2);

        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
            }
        });

        mListview = (PullableListView) findViewById(R.id.listview);
        mRefreshView = (PullToRefreshLayout) findViewById(R.id.refresh_view);

        mListview.addHeaderView(headview);

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getData();
            }
        });
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {
        mTimeShopAdapter = new TimeShopAdapter(getContext(), mDataLists);
        mListview.setAdapter(mTimeShopAdapter);

    }

    /**
     * "miao_start":秒杀开始时间（几点）
     * "miao_stop":秒杀结束时间（几点）
     * "seconds":倒计时剩余秒数
     * "status": 1未开始2正在抢购3已抢完
     */

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        apiDataRepository.msList(map, new ApiNetResponse<TimeShopBean>() {
            @Override
            public void onSuccess(TimeShopBean datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                showBanner(datas.slide);
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas.list && datas.list.size() > 0) {
                        mDataLists.addAll(datas.list);
                    } else {
                    }
                } else {
                    if (null != datas.list && datas.list.size() > 0) {
                        mDataLists.addAll(datas.list);
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mTimeShopAdapter.pushData(mDataLists);
//                time.setText(datas.miao_start+"-"+datas.miao_stop);
                switch (datas.status) {
                    case "1":
                        status.setText("未开始");
                        break;
                    case "2":
                        status.setText("正在抢购");
                        break;
                    case "3":
                        status.setText("已抢完");
                        break;
                }
                timer = datas.seconds;
                setTime(datas.seconds);
                try {

                    hour1.setText(getTime(datas.seconds).charAt(0) + "");
                    hour2.setText(getTime(datas.seconds).charAt(1) + "");
                    min1.setText(getTime(datas.seconds).charAt(3) + "");
                    min2.setText(getTime(datas.seconds).charAt(4) + "");
                    s1.setText(getTime(datas.seconds).charAt(6) + "");
                    s2.setText(getTime(datas.seconds).charAt(7) + "");
                } catch (Exception e) {

                }
            }
        });
    }

    public void showBanner(List<HomePageIndex.SlideBean> banners) {
        if (banners == null || banners.size() < 1) {
            return;
        }
        time_slide.setImageResources(banners, new ImageCycleView.ImageCycleViewListener() {
            @Override
            public void displayImage(String imageURL, ImageView imageView) {
                //根据图片地址，设置imageView
                // 使用ImageLoader对图片进行加装！
                GlideUtil.showImg(getActivity(), imageURL, imageView);
            }

            @Override
            public void onImageClick(HomePageIndex.SlideBean mEntity, int postion, View imageView) {
            }
        });
    }

    public String getTime(int second) {
        if (second < 10) {
            return "00:00:0" + second;
        }
        if (second < 60) {
            return "00:00:" + second;
        }
        if (second < 3600) {
            int minute = second / 60;
            second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "00:0" + minute + ":0" + second;
                }
                return "00:0" + minute + ":" + second;
            }
            if (second < 10) {
                return "00:" + minute + ":0" + second;
            }
            return "00:" + minute + ":" + second;
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + ":0" + minute + ":0" + second;
                }
                return "0" + hour + ":0" + minute + ":" + second;
            }
            if (second < 10) {
                return "0" + hour + minute + ":0" + second;
            }
            return "0" + hour + ":" + minute + ":" + second;
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + ":0" + minute + ":0" + second;
            }
            return hour + ":0" + minute + ":" + second;
        }
        if (second < 10) {
            return hour + ":" + minute + ":0" + second;
        }
        Log.i("xugj", hour + ":" + minute + ":" + second);
        return hour + ":" + minute + ":" + second;
    }


    public String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


    private void setTime(long timer) {
            if(countDownTimer==null){
                countDownTimer = new CountDownTimer(99999999, 1000) {
                    public void onTick(long millisUntilFinished) {
                        handler.sendEmptyMessage(0);
                    }

                    public void onFinish() {

                    }
                }.start();
            }

    }
}
